# frozen_string_literal: true

module Pageable
  DEFAULT_PAGE_SIZE = 10
  DEFAULT_PAGE_NO = 0

  extend ActiveSupport::Concern
  included do
    scope :asc_by, ->(field) { order field }
    scope :desc_by, ->(field) { order field => :desc }

    scope :page_info, lambda { |page_size, page_no|
      page_size = page_size.to_i
      page_no = page_no.to_i
      page_size = DEFAULT_PAGE_SIZE unless page_size.positive?
      page_no = DEFAULT_PAGE_NO if page_no.negative?
      offset(page_size * page_no).limit(page_size)
    }
  end
end
